<?php
set_include_path($_SERVER ['DOCUMENT_ROOT']);
include_once 'admin/includes/functions/admin-functions.php';
global $DBobject, $SMARTY;

if (empty($_SESSION['user']['admin'])) {
    echo json_encode([
      'template' => "您的登陆状态已经过期。 请重新<a href='/admin/login'>登陆</a>。",
      'term' => 0,
      'termlist' => 0,
    ]);
    die();
}

$referer = parse_url($_SERVER['HTTP_REFERER']);
if ($referer['host'] == $GLOBALS['HTTP_HOST'] && !empty($_SESSION['user']['admin']['id'])) {
    //TO SYNC FROM PREVIOUS TERMS
    if ($_POST['action'] == 'GetClassTerm' && !empty($_POST['term_region'])) {
        $sql = "SELECT tbl_agegroup.agegroup_shortname, tbl_class.class_object_id, tbl_class.class_name, tbl_class.class_weekday, tbl_class.class_start_time FROM tbl_class LEFT JOIN tbl_agegroup ON class_agegroup_id = agegroup_id WHERE class_deleted IS NULL AND class_published = '1' AND agegroup_deleted IS NULL ORDER BY FIELD(class_weekday,'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'), class_start_time";
        if ($classes = $DBobject->wrappedSql($sql)) {
            //var_dump($classes);exit;
            if (!empty($_POST['term_id'])) {
                $sql = 'SELECT classterm_class_id, classterm_published FROM tbl_classterm WHERE classterm_deleted IS NULL AND classterm_term_id = :classterm_term_id ';
                if ($classterms = $DBobject->wrappedSql($sql, array(':classterm_term_id' => $_POST['term_id']))) {
                    foreach ($classterms as $ct) {
                        foreach ($classes as &$c) {
                            if ($ct['classterm_class_id'] == $c['class_object_id']) {
                                $c['classterm_published'] = $ct['classterm_published'];
                                break;
                            }
                        }
                    }
                }
            }
            $SMARTY->assign('classes', $classes);
            $template = $SMARTY->fetch('edit_classterm.tpl');
        }

        if (empty($_POST['sync'])) {
            //GET TERM LIST FILTERED BY REGION
            $sql = 'SELECT term_id, term_name FROM tbl_term WHERE term_deleted IS NULL AND term_published = 1 AND term_region = :term_region ORDER BY term_id';
            $termlist = array();
            if ($res = $DBobject->wrappedSql($sql, array(':term_region' => $_POST['term_region']))) {
                foreach ($res as $r) {
                    $termlist[] = array('id' => $r['term_id'], 'value' => $r['term_name']);
                }
            }
        }
        echo json_encode(array(
                'template' => $template,
                'term' => $_POST['term_id'],
                'termlist' => $termlist,
        ));
        die();
    }

    //PROCESS CLASSTERMS RECORDS
    if ($_POST['action'] == 'ProcessClassterms' && !empty($_POST['term_id']) && !empty($_POST['classterms'])) {
        //UNPUBLISH ALL CLASSTERM
        $sql = "UPDATE tbl_classterm SET classterm_published = '0', classterm_modified = NOW() WHERE classterm_deleted IS NULL AND classterm_term_id = :classterm_term_id ";
        $DBobject->wrappedSql($sql, array(':classterm_term_id' => $_POST['term_id']));
        $classterm = array();
        foreach ($_POST['classterms'] as $ctoid => $val) {
            if (empty($val)) {
                continue;
            }
            //CHECK FOR EXISTING CLASSTERM
            $sql = 'SELECT classterm_id FROM tbl_classterm WHERE classterm_deleted IS NULL AND classterm_term_id = :classterm_term_id AND classterm_class_id = :classterm_class_id ';
            if ($res = $DBobject->wrappedSql($sql, array(':classterm_term_id' => $_POST['term_id'], ':classterm_class_id' => $ctoid))) {
                //UPDATE EXISTING CLASSTERM
                $sql = "UPDATE tbl_classterm SET classterm_published = '1', classterm_modified = NOW() WHERE classterm_id = :classterm_id ";
                $DBobject->wrappedSql($sql, array(':classterm_id' => $res[0]['classterm_id']));
                $classterm[] = $res[0]['classterm_id'];
            } else {
                //INSERT NEW CLASSTERM
                $sql = 'INSERT INTO tbl_classterm (classterm_term_id, classterm_class_id, classterm_published, classterm_created) 
						VALUES ( :classterm_term_id, :classterm_class_id, 1, NOW() ) ';
                $DBobject->wrappedSql($sql, array(':classterm_term_id' => $_POST['term_id'], ':classterm_class_id' => $ctoid));
                $classterm[] = $DBobject->wrappedSqlIdentity();
            }
        }
        echo json_encode(array(
                'classterm' => $classterm,
                'term' => $_POST['term_id'],
                'notice' => 'edited',
        ));
        die();
    }
}
echo json_encode(array('notice' => 'error'));
die();
